// pages/chupiao/chupiao.js
var utils = require('../../utils/api.js');
var base64 = require('../../utils/base64.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 99, //0:部分出票成功 1:出票成功 2:幸运赠送 3:出票等待中 4:出票失败 99:未有状态页面
    orderId: '', //用户订单
    totalCount: 0, //购买数量
    finishCount: 0, //完成数量
    errorCount: 0, //失败数量
    moroCoin: 0, //魔彩币
    errorTime: 100, //超时时间
    errorCount: 0, //错误次数
    maxerrorCount: 5, //最大错误次数,
    timingRotation: 2000, //定时轮询时间
    intervalError: true, //错误轮询标记
    intervalSuccess: true, //正确轮询标记
    overtimeStamp: true, //超时标记
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    that.setData({
      orderId: options.orderId
    })
    that.selectLotteryResult()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this
    that.data.overtimeStamp = setInterval(function () {
      console.log(that.data.errorTime)
      that.setData({ errorTime: that.data.errorTime - 1, })
      if (that.data.errorTime <= 0) {
        clearInterval(that.data.overtimeStamp);
        wx.showLoading({
          title: '请求超时',
          mask: true,
        })
        setTimeout(function () {
          wx.hideLoading()
        }, that.data.timingRotation)
        //跳转之前清楚定时器
        clearTimeout(that.data.intervalSuccess)
        clearTimeout(that.data.intervalError)
        //计时结束跳转
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/goucai/goucai',
          })
        }, that.data.timingRotation)
      }
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  //查询用户出票结果
  selectLotteryResult: function () {
    var that = this
    wx.request({
      url: getApp().globalData.requestUrl + '/getTicketResult',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"orderId":"' + that.data.orderId + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.statusCode == 200) {
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log(dat.dataList)
            if (dat.status == 0) {
              if (dat.dataList.totalCount == '001') {
                console.log('出票中')
                if (that.data.type != 3)
                  that.setData({ type: 3, })
                that.data.intervalSuccess = setTimeout(function () {
                  console.log('intervalSuccess')
                  that.selectLotteryResult()
                }, that.data.timingRotation)
              } else {
                if (parseInt(dat.dataList.totalCount) == (parseInt(dat.dataList.finishCount) + parseInt(dat.dataList.errorCount))) {
                  if (parseInt(dat.dataList.errorCount) > 0) { //出票错误
                    if (parseInt(dat.dataList.finishCount) > 0) { //部分出票失败
                      that.setData({
                        type: 0
                      })
                    } else {
                      that.setData({ //全部出票失败
                        type: 4
                      })
                    }
                  } else { //全部完成
                    that.setData({
                      type: 1
                    })
                  }
                  //设置出票张数
                  that.setData({
                    moroCoin: (dat.dataList.moroCoin == '') ? 0 : dat.dataList.moroCoin,
                  })
                  clearTimeout(that.data.intervalSuccess)
                  clearTimeout(that.data.intervalError)
                  clearInterval(that.data.overtimeStamp)
                  //返回到购彩界面
                  wx.showToast({
                    title: '5秒后即将返回',
                    mask: true,
                    icon: 'none',
                    duration: 3000,
                  })
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/goucai/goucai'
                    })
                  }, 5000)
                }
              }
            } else { //服务器返回数据错误
            }
          } else { //微信请求成功没有数据
            that.data.intervalError = setTimeout(function () {
              console.log('intervalError')
              that.selectLotteryResult()
            }, that.data.timingRotation)
          }
        } else { //微信请求失败
          that.data.intervalError = setTimeout(function () {
            console.log('intervalError')
            that.selectLotteryResult()
          }, that.data.timingRotation)
        }
      },
      fail: function (res) {
      },
    })
  }
})